package smartcampus.scjava.controller.forth;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.Book;
import smartcampus.scjava.POJO.DO.BookBorrow;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.IBookBorrowService;
import smartcampus.scjava.service.IBookService;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 书籍借阅表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-11-22
 */
@RestController
@RequestMapping("/book-borrow")
public class BookBorrowController {

    @Resource
    private IBookBorrowService bookBorrowService;

    @Resource
    private IBookService bookService;


    @RequestMapping("/listPage")
    public ResultVO<?> listPageBook(Integer  pageNum,Integer pageSize, String word,String userId) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }
        if (userId == null || userId.isEmpty()) {
            return ResultVO.error("userId不能为空");
        }

        Page<BookBorrow> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BookBorrow> query = new QueryWrapper<>();
        query.lambda().eq(BookBorrow::getUserId,userId).like(BookBorrow::getBookName,word).
                or().like(BookBorrow::getBookNo,word);

        bookBorrowService.page(page,query);

        return ResultVO.success(page);
    }


    @RequestMapping("/add")
    public ResultVO<?> add(@RequestBody BookBorrow borrow) {

        QueryWrapper<BookBorrow> query = new QueryWrapper<>();
        query.eq("book_no",borrow.getBookNo());
        query.eq("status",0);
        long count = bookBorrowService.count(query);
        if (count > 0) {
            return ResultVO.fail("借书失败！已被借用未归还");
        }

        QueryWrapper<Book> query1 = new QueryWrapper<>();
        query1.lambda().eq(Book::getBookNo,borrow.getBookNo());
        Book one = bookService.getOne(query1);
        if (one == null) {
            return ResultVO.fail("借书失败！书籍不存在");
        }

        borrow.setBorrowDay((int) LocalDateTimeUtil.between(borrow.getBeginDate().atStartOfDay(),borrow.getEndDate().atStartOfDay(), ChronoUnit.DAYS));
        borrow.setBookName(one.getBookName());
        borrow.setCreateTime(LocalDateTimeUtil.now());
        borrow.setUpdateTime(LocalDateTimeUtil.now());
        bookBorrowService.save(borrow);

        return ResultVO.success("借书成功！");
    }

    @RequestMapping("/returnBook")
    public ResultVO<?> returnBoot(Integer borrowId) {
        BookBorrow byId = bookBorrowService.getById(borrowId);
        if (byId == null) {
            return ResultVO.fail("未找到借阅信息");
        } else {
            byId.setStatus(1);
            bookBorrowService.updateById(byId);
            return ResultVO.success("归还成功");
        }
    }

    @RequestMapping("/returnBookAll")
    public ResultVO<?> returnAllBoot(Integer borrowId) {
        BookBorrow byId = bookBorrowService.getById(borrowId);
        if (byId == null) {
            return ResultVO.fail("未找到借阅信息");
        } else {
            byId.setStatus(1);
            bookBorrowService.updateById(byId);
            return ResultVO.success("归还成功");
        }
    }

}
